#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Git Push Helpers ===
alias gitpf='git push -f';

# === Git Push Validated Remote ===
function gitpu()
{
  # Usage: gitpu [remote] [branch] [input] (Git push to validated remote)

  # Variables
  local remote=${1:-github};
  local branch=${2:-$(repogetbranch)};
  local input=${3};
  local cmd;

  # Get remote stats
  echo '';
  gitstat "${remote}" "${branch}" true;

  # Introduction
  echo '';
  local cmd="git push -f ${remote} HEAD:refs/heads/${branch}";
  echo -n "  ${cmd} [Y/n] ? ";
  if [ -z "${input}" ]; then
    read -r key;
  else
    key=${input};
    echo "${key}";
  fi;
  echo '';

  # Abort push
  if [[ "${key}" == 'n' ]] || [[ "${key}" == 'N' ]]; then
    return;
  fi;

  # Fetch and push
  git fetch "${remote}" "${branch}";
  ${cmd};
  echo '';
}

# === Git Full Url Push ===
function gitpurl()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: gitpurl <url> (Git pull to full url)';
    echo '';
    return;
  fi;

  # Variables
  local url=${1};
  local branch;

  # Prepare url
  branch=${url};
  url=${url%/commits/*};
  url=${url%/tree/*};
  branch=${branch#*/commits/};
  branch=${branch#*/tree/};

  # Git push to full url
  gitpu "${url}" "${branch}";
}

# === Git Push Validated Helpers ===
alias gitpa='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername)';
alias gitpa14='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) cm-14.1';
alias gitpa15='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) lineage-15.0';
alias gitpala='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) LA.UM.5.5_rb1.10';
alias gitpal='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) local_manifests'
alias gitpaman='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) manifests'
alias gitpam='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) multirom';
alias gitpama='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) master';
alias gitpanmr='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) n-mr1'
alias gitpan='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) nougat';
alias gitpat='gitremoteverify $(githubusername) gitraa && gitpu $(githubusername) twrp';
alias gitpo='gitremoteverify origin false && gitpu origin';
alias gitpom='gitremoteverify origin false && gitpu origin master';
alias gitpon='gitremoteverify next gitraon && gitpu next lineage-15.0_master';
alias gitpp='gitremoteverify project gitrap && gitpu project';
alias gitppm='gitremoteverify project gitrap && gitpu project master';
alias gitppriv='gitremoteverify private false && gitpu private';
alias gitpprivm='gitremoteverify private false && gitpu private master';

# === Git Push Automated Helper ===
alias gitpoa='gitpu $(gitgetremote $(repogetbranch)) $(repogetbranch)';
